//
//  IncomeTableView.m
//  Cat
//
//  Created by lushitong on 2018/12/5.
//  Copyright © 2018年 lushitong. All rights reserved.
//

#import "IncomeTableView.h"
#import "WebViewController.h"
#import "HttpWebManager.h"
#import "HttpUserManager.h"
#import "IncomeTopTableViewCell.h"
#import "DetailsViewController.h"

@interface IncomeTableView()

@property (nonatomic, strong) EarnModel *model;

@end

@implementation IncomeTableView

- (instancetype)initWithFrame:(CGRect)frame sections:(NSInteger)sections {
	if (self = [super initWithFrame:frame sections:sections]) {
		self.addRefresh = NO;
		self.backgroundColor = [UIColor whiteColor];
		self.tableView.backgroundColor = [UIColor whiteColor];
		self.tableView.showsVerticalScrollIndicator = NO;
		self.noFirstAutoRequest = NO;
		self.outerControlLoading = NO;
		self.loadingBgColor = [UIColor whiteColor];
		[self loadView];
	}
	return self;
}

- (void)sendRequest {
	[HttpUserManager getEarnInfo:^(EarnModel * _Nonnull model) {
		self.model = model;
		dispatch_async(dispatch_get_main_queue(), ^{
			[self.tableView reloadData];
		});
		[self finishLoadData:@[]];
	} failure:^(NSInteger code, NSString * _Nonnull message) {
		[FLProgressHUD showError:message];
	}];
}

- (void)loadView {
	[self p_setupTopCell];
	[self p_setupInfoCell];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	UITableViewCell *sectionCell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
	if (indexPath.section == 0) {
		if (indexPath.row == 1) {
			sectionCell = [self cellWithoutAnything];
		}else{
			IncomeTopTableViewCell *cell = (IncomeTopTableViewCell *)sectionCell;
			cell.cellModelData = self.model;
		}
	}
	return sectionCell;
}

- (UITableViewCell *)cellWithoutAnything {
	static NSString *identifier = @"QKProfileNothingCell";
	UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:identifier];
	if (!cell) {
		cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
	}
	cell.selectionStyle = UITableViewCellSelectionStyleNone;
	cell.contentView.backgroundColor = COLOR_WITH_HEX(0xF3F3F3);
	return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	CGFloat cellHeight  = [super tableView:tableView heightForRowAtIndexPath:indexPath];
	if (indexPath.section == 0) {
		if (indexPath.row == 1) {
			cellHeight = kQKFitModuleDecimal(8);
		} else {
			cellHeight = kQKFitModuleDecimal(370);
		}
	} else if (indexPath.section == 1) {
		cellHeight = kQKFitModuleDecimal(50);
	}
	return cellHeight;
}

- (void)p_setupTopCell {
	QKBaseTableCellModel *meTopCellData = [[QKBaseTableCellModel alloc] init];
	meTopCellData.isDynamic = YES;
	meTopCellData.cellClass = @"IncomeTopTableViewCell";
	meTopCellData.section = 0;
	meTopCellData.cellData = @[@"",@""];
	meTopCellData.cellHeight = kQKFitModule(370);
	[self addTableSectionData:meTopCellData];
}

- (void)p_setupInfoCell {
	QKBaseTableCellModel *meTopCellData = [[QKBaseTableCellModel alloc] init];
	meTopCellData.isDynamic = NO;
	meTopCellData.cellClass = @"IncomeInfoTableViewCell";
	meTopCellData.section = 1;
	meTopCellData.cellHeight = kQKFitModule(50);
	[self addTableSectionData:meTopCellData];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	if (indexPath.section == 0) {
		DetailsViewController *details = [[DetailsViewController alloc] init];
		[self.currentNavigationController pushViewController:details animated:YES];
	} else if (indexPath.section == 1) {
		WebViewController *vc = [[WebViewController alloc] init];
		vc.URL = [HttpWebManager getCrashRuler];
		[self.currentNavigationController pushViewController:vc animated:YES];
	}
}

@end
